package cn.iocoder.yudao.module.dbit.controller.admin.majinfo;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.majinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.majinfo.MajInfoDO;
import cn.iocoder.yudao.module.dbit.service.majinfo.MajInfoService;

@Tag(name = "管理后台 - 共建专业")
@RestController
@RequestMapping("/dbit/maj-info")
@Validated
public class MajInfoController {

    @Resource
    private MajInfoService majInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建共建专业")
    @PreAuthorize("@ss.hasPermission('dbit:maj-info:create')")
    public CommonResult<Integer> createMajInfo(@Valid @RequestBody MajInfoSaveReqVO createReqVO) {
        return success(majInfoService.createMajInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新共建专业")
    @PreAuthorize("@ss.hasPermission('dbit:maj-info:update')")
    public CommonResult<Boolean> updateMajInfo(@Valid @RequestBody MajInfoSaveReqVO updateReqVO) {
        majInfoService.updateMajInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除共建专业")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:maj-info:delete')")
    public CommonResult<Boolean> deleteMajInfo(@RequestParam("id") Integer id) {
        majInfoService.deleteMajInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得共建专业")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MajInfoRespVO> getMajInfo(@RequestParam("id") Integer id) {
        MajInfoDO majInfo = majInfoService.getMajInfo(id);
        return success(BeanUtils.toBean(majInfo, MajInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得共建专业分页")
    @PreAuthorize("@ss.hasPermission('dbit:maj-info:query')")
    public CommonResult<PageResult<MajInfoRespVO>> getMajInfoPage(@Valid MajInfoPageReqVO pageReqVO) {
        PageResult<MajInfoDO> pageResult = majInfoService.getMajInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MajInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出共建专业 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:maj-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMajInfoExcel(@Valid MajInfoPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MajInfoDO> list = majInfoService.getMajInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "共建专业.xls", "数据", MajInfoRespVO.class,
                BeanUtils.toBean(list, MajInfoRespVO.class));
    }

}